package com.woniuxy.main.dynamic_lzq.hander;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.woniuxy.main.dynamic_lzq.dto.UserCoachDto;
import com.woniuxy.main.dynamic_lzq.pojo.Coach;
import com.woniuxy.main.dynamic_lzq.pojo.Course;
import com.woniuxy.main.dynamic_lzq.pojo.User;
import com.woniuxy.main.dynamic_lzq.service.CoachService_Lzq;
import com.woniuxy.main.dynamic_lzq.service.UserService_Lzq;
import com.woniuxy.main.dynamic_lzq.test.MapDistance;
import com.woniuxy.main.login_yjj.pojo.User_Yjj;
import com.woniuxy.main.login_yjj.service.LoginService;

@Controller
public class CoachHandler_Lzq {
	@Autowired
	private CoachService_Lzq coachService;
	@Autowired
	private UserService_Lzq userService;
	
	@Autowired
	private LoginService loginService;
	@RequestMapping("/showcoachIndistance")
	@ResponseBody
	public UserCoachDto showCoachIndistance(HttpServletRequest request){
		User_Yjj currentUser =  (User_Yjj) request.getSession().getAttribute("currentUser");
		//用户所在经度、纬度
		double  userLongitude=currentUser.getLongitude();	
		double userLatitude=currentUser.getLatitude();
		
		List<User> users = userService.listCoaches();
		
		
		 coachDistance(userLongitude,userLatitude,users);
		
		 users = sortDistance();
		 List<Coach> coaches = coachService.findCoachesByUsers(users);
		 UserCoachDto dto = new UserCoachDto(users, coaches);
		 return dto;
	}
	
	@RequestMapping("/showcoachInprice")
	@ResponseBody
	public UserCoachDto showCoachInPrice(HttpServletRequest request){
		User_Yjj currentUser =  (User_Yjj) request.getSession().getAttribute("currentUser");
		//用户所在经度、纬度
				double  userLongitude=currentUser.getLongitude();	
				double userLatitude=currentUser.getLatitude();
		 
		 List<Coach> coaches = coachService.listCoachesByPrice();
		 List<User> users = userService.findUsersByCoaches(coaches);
		 UserCoachDto dto = new UserCoachDto(users, coaches);
		 coachDistance(userLongitude,userLatitude,users);
		 //System.out.println(coaches.toString());
		 return dto;
	}
	
	
	
	
	static TreeMap<Double, User> distanceMap = new TreeMap<Double, User>();
	//遍历教练距学员的距离
	public static void coachDistance(double userLongitude,double userLatitude,List<User> users){
		for(int i=0;i<users.size();i++){
			User user = users.get(i);
			double coachLongitude=user.getLongitude();
			double coachLatityde = user.getLatitude();
			//source的经纬度，target的经纬度
			double distance =  MapDistance.findDistance(userLongitude,userLatitude,coachLongitude,coachLatityde);
			user.setDistance(distance);
			
			distanceMap.put(distance, user);
		}
	}
	
	//按教练距离进行排序
	private static List<User> sortDistance() {
		List<User> users = new ArrayList<>();
	    Iterator<Entry<Double, User>> it = distanceMap.entrySet().iterator();
        while (it.hasNext()) {
            Entry<Double, User> entry = it.next();
          /*  System.out.println("key = " + entry.getKey() + ", value = " + entry.getValue());*/
            users.add(entry.getValue());
        }
		return users;
	}
	
	@RequestMapping("/showInfo")
	public ModelAndView showInfo(int user_id){
		ModelAndView modelAndView  = new ModelAndView();
		User user  = userService.findUserByUid(user_id);
		modelAndView.addObject("user", user);
		modelAndView.setViewName("coachdetail.html");
		return modelAndView;
	}
	
	@RequestMapping("/choseclass")
	public ModelAndView choseclass(int user_id){
		ModelAndView modelAndView  = new ModelAndView();
		User user = userService.findUserByUid(user_id);
		Coach coach = coachService.findCoachByUid(user_id);
		List<Course> courses = coachService.findCoursesByCid(coach.getCoach_id());
		
		modelAndView.addObject("user", user);
		modelAndView.addObject("coach", coach);
		modelAndView.addObject("courses", courses);
		modelAndView.setViewName("choseclass.html");
		return modelAndView;
	}
	
	@RequestMapping("/test")
	public void test(Coach coach,Course course,String date,String time,int price){
		System.out.println(coach.getCoach_id());
		System.out.println(coach.getVenue_id());
		System.out.println(date);
		System.out.println(time);
		System.out.println(price);
		DateFormat format = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
		String str = date+" "+time;
		try {
			Date choseDate = format.parse(str);
			System.out.println(choseDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
	}
}
